package proyecto.testcasetool.presentation.details;


import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IPartSelectionListener;
import org.eclipse.ui.forms.widgets.FormToolkit;

import proyecto.testcasetool.presentation.editor.pages.FormLayoutFactory;


public abstract class DetailsPage extends AbstractFormPart implements IDetailsPage, IPartSelectionListener/*, IPropertiesEditionListener*/ {

	protected FormToolkit toolkit;

	protected EditingDomain editingDomain;

	protected EObject eObject;

	protected AdapterFactory adapterFactory;

	public DetailsPage(FormToolkit toolkit, EditingDomain editingDomain, AdapterFactory adapterFactory) {
		super();
		this.toolkit = toolkit;
		this.editingDomain = editingDomain;
		this.adapterFactory = adapterFactory;
	}

	public void createContents(Composite parent) {
		toolkit = getManagedForm().getToolkit();
		parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		parent.setLayoutData(new GridData(GridData.FILL_BOTH));	
		
		createDetailsSection(parent);
		
//		messageManager = new PropertiesEditionMessageManager() {
//
//			@Override
//			protected void updateStatus(String message) {
//				if (message != null && !"".equals(message))
//					getManagedForm().getForm().setMessage(message, IMessageProvider.ERROR);
//				else
//					getManagedForm().getForm().setMessage(null, IMessageProvider.NONE);
//			}
//		};
//		this.viewer = new PropertiesEditionViewer(container, null, SWT.NONE, 1);
//		viewer.setDynamicTabHeader(true);
//		viewer.setToolkit(getManagedForm().getToolkit());
//		viewer.setContentProvider(new PropertiesEditionContentProvider(adapterFactory,
//				IPropertiesEditionComponent.LIVE_MODE, editingDomain));
//		viewer.addPropertiesListener(this);
	}

	public void selectionChanged(IFormPart part, ISelection selection) {
		// controlar que sea la part que esto esperando!!!!
		if (!(selection instanceof IStructuredSelection)) {
			return;
		}
		EObject newEObject = getEObjectFromSelection(selection);
		if (newEObject != null && newEObject != eObject) {
			eObject = newEObject;
			if (eObject != null) {
				System.out.println("DetailsPage.selectionChanged"+
						"\n"+part+"\n"+selection);
//				if (viewer.getToolkit() == null)
//					viewer.setToolkit(toolkit);
//				viewer.setInput(new DomainPropertiesEditionContext(null, null, editingDomain, adapterFactory,
//						eObject));
//				viewer.addPropertiesListener(this);
			}
		}
	}

	protected EObject getEObjectFromSelection(ISelection selection) {
		if (selection instanceof StructuredSelection
				&& (((StructuredSelection)selection).getFirstElement() instanceof EObject))
			return (EObject)((StructuredSelection)selection).getFirstElement();
		if (selection instanceof EObject)
			return (EObject)selection;
		if (selection instanceof IAdaptable && ((IAdaptable)selection).getAdapter(EObject.class) != null)
			return (EObject)((IAdaptable)selection).getAdapter(EObject.class);

		return null;
	}

	/**
	 * @return the viewer
	 */
//	public PropertiesEditionViewer getViewer() {
//		return viewer;
//	}

//	public void firePropertiesChanged(IPropertiesEditionEvent event) {
//		handleChange(event);
//		if (event.getState() == PropertiesEditionEvent.FOCUS_CHANGED
//				&& event.getKind() == PropertiesEditionEvent.FOCUS_GAINED) {
//			// de-activate global actions
//			if (getEditor() instanceof InteractiveEEFEditor) {
//				((InteractiveEEFEditor)getEditor()).deactivateCCPActions();
//			}
//		} else if (event.getState() == PropertiesEditionEvent.FOCUS_CHANGED
//				&& event.getKind() == PropertiesEditionEvent.FOCUS_LOST) {
//			// re-activate global actions
//			if (getEditor() instanceof InteractiveEEFEditor) {
//				((InteractiveEEFEditor)getEditor()).activateCCPActions();
//			}
//		}
//	}

//	private void handleChange(IPropertiesEditionEvent event) {
//		// do not handle changes if you are in initialization.
//		if (viewer.isInitializing())
//			return;
//		messageManager.processMessage(event);
//	}

	/**
	 * Retrieve the Editor from the form.
	 * 
	 * @return The eef editor used to display this page.
	 */
//	private Object getEditor() {
//		if (getManagedForm().getContainer() instanceof AbstractMDFormPage)
//			return ((AbstractMDFormPage)getManagedForm().getContainer()).getEditor();
//		return null;
//	}

	protected abstract void createDetailsSection(Composite parent);
}
